package com.itwn.controller;


import com.github.pagehelper.PageInfo;
import com.itwn.entity.UrlPermission;
import com.itwn.result.ResponseResult;
import com.itwn.service.UrlPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("UrlPermission")
@CrossOrigin
@Api(value = "权限管理")
public class UrlPermissionController {


    @Autowired
     private UrlPermissionService urlPermissionService;

    @ApiOperation(value = "权限列表显示")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    paramType="query",name="pageNum",value="分页参数的当前页",
                    dataType="int",required=false),
            @ApiImplicitParam(
                    paramType="query",name="pageSize",value="分页参数的每页查询条数",
                    dataType="int",required=false),
            @ApiImplicitParam(
                    paramType="query",name="name",value="权限名称",
                    dataType="String",required=false)
    })
    @GetMapping("/PageList")
    public PageInfo<UrlPermission> findWithPage(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                                                String name){
        PageInfo<UrlPermission> pageInfo = urlPermissionService.findWithPage(pageNum, pageSize,name);
        return pageInfo;
    }

    @PostMapping("/updatePer")
    @ApiOperation(value = "修改权限")
    public ResponseResult<UrlPermission> updatePer(@RequestBody UrlPermission urlPermission){

        ResponseResult<UrlPermission> result = new ResponseResult<>();
       int i= urlPermissionService.updatePer(urlPermission);
        if (i == 1){
            result.setCode(200);
            result.setMsg("修改讲师数据成功!");
            result.setData(urlPermission);
            return result ;
        }else {
            result.setCode(101);
            result.setMsg("修改讲师数据失败!");
            return result;
        }
    }

    @PostMapping("/addPer")
    @ApiOperation(value = "权限添加操作")
    public ResponseResult<UrlPermission> add(@RequestBody UrlPermission urlPermission){
        ResponseResult<UrlPermission> result = new ResponseResult<>();
        int i= urlPermissionService.addPer(urlPermission);
        if (i == 1){
            result.setCode(200);
            result.setData(urlPermission);
            return result ;
        }else {
            result.setCode(101);
            return result;
        }
    }


    @GetMapping("/allRoleMenuList")
    @ApiOperation(value = "查询所有角色列表")
    public ResponseResult<List<UrlPermission>> allRoleMenuList(){
        ResponseResult<List<UrlPermission>> result = new ResponseResult<>();
        List<UrlPermission> urlPermissionList=  urlPermissionService.findList();
        result.setData(urlPermissionList);
        return result;
    }


    @PostMapping("/removePermission/{id}")
    @ApiOperation(value = "权限删除")
    @ApiImplicitParam(
            paramType = "path",name="id",value = "权限的id",
            dataType = "int",required = true)
    public ResponseResult<UrlPermission> removePermission(@PathVariable("id")long id){
        ResponseResult<UrlPermission> result = new ResponseResult<>();
        int i= urlPermissionService.removePermission(id);
        if (i == 1){
            result.setCode(200);
            return result ;
        }else {
            result.setCode(101);
            return result;
        }
    }




}
